package com.jackrain.nea.fc.cp.mapper;

import com.jackrain.nea.fc.utils.ObjectToUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface EmpInfoMapper {

     @SelectProvider(type = EmpInfoSql.class, method = "findEmpInfoData")
     List<HashMap> selEmpInfo(@Param("date_beg") String date_beg,
                              @Param("date_end") String date_end,
                              @Param("dept_no")String dept_no,
                              @Param("code_name")String code_name);

    class EmpInfoSql {

        public String findEmpInfoData(Map<String, Object> map) {
            String code_name = (String) map.get("code_name");
            String date_beg = (String) map.get("date_beg");
            String date_end = (String) map.get("date_end");
            String dept_no = (String) map.get("dept_no");
            return new SQL() {
                {
                    SELECT("a.ECODE as emp_no," +
                            "a.ENAME as emp_name," +
                            "b.ecode as dept_no," +
                            "b.ename as dept_name," +
                            "a.ENUMNO as saler_no," +
                            "a.ESHORTNO as picker_no," +
                            "c.mixname as region," +
                            "d.mixname as store," +
                            "a.sex," +
                            "a.ISSALER as is_saler," +
                            "a.ISPICKER as is_picker," +
                            "a.BIRTHDATE as birthdate," +
                            "a.ID_CARD as id_card," +
                            "a.hiredate as hire_date," +
                            "a.education as education," +
                            "e.ename as postgrade," +
                            "f.ename as job," +
                            "g.ename as job_rank," +
                            "a.EMP_POST as post," +
                            "a.ISOFFICIAL  as is_officaial," +
                            "a.officialdate as official_date," +
                            "a.isleave as is_leave," +
                            "a.leavedate as leave_date," +
                            "a.mobil as mobil," +
                            "a.tel as tel," +
                            "a.fax as fax," +
                            "a.email as email," +
                            "a.address as address ");
                    FROM("cp_c_emp a " +
                            "   JOIN cp_c_hrorg b on a.CP_C_HRORG_ID = b.id" +
                            "   left join cp_c_storeorg c on a.CP_C_DISTRIB_ID = c.id" +
                            "   left join cp_c_store d on a.CP_C_STORE_ID = d.id" +
                            "   left join CP_C_POSTGRADE e on a.CP_C_POSTGRADE_ID = e.ID" +
                            "   left join CP_C_JOB f on a.CP_C_JOB_ID = f.ID" +
                            "   left join CP_C_JOBRANK g on a.CP_C_JOBRANK_ID = g.ID ");
                    WHERE("a.isactive = 'Y' " +
                            "   and a.MODIFIEDDATE BETWEEN #{date_beg} and #{date_end}");
                    if (!ObjectToUtil.isNullOREmpty(dept_no)) {
                        WHERE("b.ECODE = " + dept_no);
                    }
                    if (!ObjectToUtil.isNullOREmpty(code_name)) {
                        WHERE("a.ECODE = " + code_name + " or a.ENAME =" + code_name);
                    }
                }
            }.toString();
        }
    }
}
